package client.ui;


import java.nio.channels.ConnectionPendingException;

import javax.swing.JOptionPane;

import proprietes.ConnectProprietes;
import client.core.validation.Validator;
import client.core.validation.matchers.IpMatcher;
import client.core.validation.matchers.PortMatcher;

public class ConnectionConfiguraiotn extends javax.swing.JFrame {
	private javax.swing.JFrame parent;
    /**
     * Creates new form ConnectionConfiguraiotn
     */
    public ConnectionConfiguraiotn(javax.swing.JFrame parent) {
    	this.parent = parent;
        initComponents();
        initValidateComponent();
    }
    private void initValidateComponent(){
    	ipValidator = new Validator(jTextFieldAddress, new IpMatcher());
    	portValidator = new Validator(jTextFieldPort, new PortMatcher());
    	
    	jTextFieldAddress.setText(ConnectProprietes.SERVER.getValue());
    	jTextFieldPort.setText(Integer.toString(ConnectProprietes.PORT.getiValue()));
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {


        jLabel1 = new javax.swing.JLabel();
        jTextFieldAddress = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldPort = new javax.swing.JTextField();
        jButtonRevert = new javax.swing.JButton();
        jButtonOK = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Server address");

        jLabel2.setText("Server port");

        jButtonRevert.setText("Revert");
        jButtonRevert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRevertActionPerformed(evt);
            }
        });

        jButtonOK.setText("Ok");
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldPort))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButtonOK, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonRevert)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonRevert)
                    .addComponent(jButtonOK))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }
    
    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
    	if(ipValidator.isValid() && portValidator.isValid()){
    		ConnectProprietes.SERVER.setValue(jTextFieldAddress.getText());
    		ConnectProprietes.PORT.setiValue(Integer.parseInt(jTextFieldPort.getText()));
    		parent.setEnabled(true);
        	this.dispose();
    	}else{
    		String stringMess;
    		if(!ipValidator.isValid()){
    			stringMess = "Ip is invalid";
    		}else{
    			stringMess = "Port is invalid";
    		}
    		JOptionPane.showMessageDialog(this, stringMess,"Notification Message",JOptionPane.WARNING_MESSAGE);
    		jTextFieldAddress.setText(ConnectProprietes.SERVER.getValue());
        	jTextFieldPort.setText(Integer.toString(ConnectProprietes.PORT.getiValue()));
    	}
    	
    }

    private void jButtonRevertActionPerformed(java.awt.event.ActionEvent evt) {
    	parent.setEnabled(true);
    	this.dispose();
    }


    private javax.swing.JButton jButtonOK;
    private javax.swing.JButton jButtonRevert;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextFieldAddress;
    private javax.swing.JTextField jTextFieldPort;
    private Validator ipValidator;
    private Validator portValidator;
    // End of variables declaration//GEN-END:variables
}
